﻿//-----------------------------------------------------------------------
// <copyright file="ServiceCollection.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: ServiceCollection.cs
// * history : created by qinchaoyue 2017-12-13 04:57:10
// </copyright>
//-----------------------------------------------------------------------

using System.Collections;
using System.Collections.Generic;

namespace Slwy.FrameCore.Dependency
{
    public class ServiceCollection : IServiceCollection
    {
        private readonly List<ServiceDescriptor> _descriptors = new List<ServiceDescriptor>();

        public IEnumerator<ServiceDescriptor> GetEnumerator()
        {
            return _descriptors.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(ServiceDescriptor item)
        {
            _descriptors.Add(item);
        }

        public void Clear()
        {
            _descriptors.Clear();
        }

        public bool Contains(ServiceDescriptor item)
        {
            return _descriptors.Contains(item);
        }

        public void CopyTo(ServiceDescriptor[] array, int arrayIndex)
        {
            _descriptors.CopyTo(array, arrayIndex);
        }

        public bool Remove(ServiceDescriptor item)
        {
            return _descriptors.Remove(item);
        }

        public int Count { get
            { return _descriptors.Count; }
        }

        public bool IsReadOnly { get { return false; } }
        
        public int IndexOf(ServiceDescriptor item)
        {
            return _descriptors.IndexOf(item);
        }

        public void Insert(int index, ServiceDescriptor item)
        {
            _descriptors.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _descriptors.RemoveAt(index);
        }

        public ServiceDescriptor this[int index]
        {
            get { return _descriptors[index]; }
            set { _descriptors[index] = value; }
        }

        public IServiceCollection Clone()
        {
            ServiceCollection collection = new ServiceCollection();
            foreach (ServiceDescriptor desciptor in this)
            {
                collection.Add(desciptor);
            }
            return collection;
        }
    }
}
